package com.tian.framework.security.config;

import com.tian.framework.security.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

//@Configuration
//@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    LoginSuccessHandlerImpl loginSuccessHandler;
    @Autowired
    FailureHandlerImpl failureHandlerImpl;
    @Autowired
    LogoutSuccessHandlerImpl logoutSuccessHandler;
    @Autowired
    EntryPointImpl entryPoint;
    @Autowired
    AccessDeniedHandlerImpl accessDeniedHandler;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserDetailsService userDetailsService;

    //忽略静态资源
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/static/**", "/favicon.ico");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 开启允许iframe 嵌套
        http.headers().frameOptions().disable();
        //启用跨域
        http.csrf().disable().cors();
        //认证
        http.authorizeRequests().anyRequest().authenticated().and()
                .formLogin()
                //指定url，可由相应的controller处理跳转到登录页如login_page.html
                //.loginPage("/login")//自定义登录url
                //指定自定义form表单请求的路径
                .loginProcessingUrl("/loginForm").usernameParameter("username").passwordParameter("password")
                //.defaultSuccessUrl("/success")
                .successForwardUrl("/success")//设置了登入登出的Handler,优先响应Handler
                .failureUrl("/fail")//设置了登入登出的Handler,优先响应Handler
                //自定义认证成功或者失败的返回json
                .successHandler(loginSuccessHandler)
                .failureHandler(failureHandlerImpl)
                //这个formLogin().permitAll()方法允许所有用户基于表单登录访问/login这个page。
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")//自定义退出url
                .logoutSuccessUrl("/login")
                .logoutSuccessHandler(logoutSuccessHandler)//设置了登入登出的Handler,优先响应Handler
                .invalidateHttpSession(true)
                .permitAll()
                .and()
                .rememberMe()// 记住我
                .rememberMeParameter("rememberMe")
                //.tokenRepository(passwordEncoder) 暂不写入数据库
                .userDetailsService(userDetailsService).tokenValiditySeconds(60 * 60 * 24);
        http.exceptionHandling()
                .authenticationEntryPoint(entryPoint)
                .accessDeniedHandler(accessDeniedHandler);

    }
    //@Override
    //public void configure(AuthenticationManagerBuilder auth) throws Exception {
    //auth.userDetailsService(userDetailsService);
    //}
}
